package ji.sky.commonintents;

import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;

import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;

public class PhoneCallActivity extends AppCompatActivity implements View.OnClickListener {

    private Button btn_phone_call_call;
    private EditText edit_phone_number;

    //1、首先声明一个数组permissions，将需要的权限都放在里面
    private String[] permissions = new String[]{Manifest.permission.RECORD_AUDIO,
            Manifest.permission.READ_EXTERNAL_STORAGE,
            Manifest.permission.CALL_PHONE
    };
    //2、创建一个mPermissionList，逐个判断哪些权限未授予，未授予的权限存储到mPerrrmissionList中
    private List<String> mPermissionList = new ArrayList<>();

    private final int mRequestCode = 100;//权限请求码
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_phone_call);
        requestPermissions();
        initWidgets();
    }

    //权限判断和申请
    protected void requestPermissions(){
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            //清空没有通过的权限
            mPermissionList.clear();

            for (int i=0;i<permissions.length;i++){
                if (ContextCompat.checkSelfPermission(this,
                        permissions[i]) != PackageManager.PERMISSION_GRANTED){
                    if(ActivityCompat.shouldShowRequestPermissionRationale(this,permissions[i])){
                        //用户彻底拒绝授予权限，一般会提示用户进入设置权限界面
                    }else{
                        //用户未彻底拒绝授予权限
                        mPermissionList.add(permissions[i]);
                    }
                }
            }
            //申请权限
            if (mPermissionList.size() > 0) {//有权限没有通过，需要申请

                ActivityCompat.requestPermissions(this, mPermissionList.toArray(new String[mPermissionList.size()]), mRequestCode);
            }else{
                //说明权限都已经通过，可以做你想做的事情去
            }
            /*
            if (ContextCompat.checkSelfPermission(this, Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) {
                // 检查权限状态
                if (ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.CALL_PHONE)) {
                    //  用户彻底拒绝授予权限，一般会提示用户进入设置权限界面
                } else {
                    //  用户未彻底拒绝授予权限
                    ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.CALL_PHONE}, 1);
                }
            }*/
        }
    }

    protected void initWidgets(){
        btn_phone_call_call = findViewById(R.id.btn_phone_call_call);
        edit_phone_number = findViewById(R.id.edit_phone_number);

        btn_phone_call_call.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        if(view.getId() == R.id.btn_phone_call_call){
            dialPhoneNumber(edit_phone_number.getText().toString());

        }
    }
    @Override
    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        if (requestCode == mRequestCode) {
            for (int i = 0; i < permissions.length; i++) {
                if (grantResults[i] == PackageManager.PERMISSION_GRANTED) {
                    // 申请成功
                    Toast.makeText(this, "" + "权限" + permissions[i] + "申请成功", Toast.LENGTH_SHORT).show();
                } else {
                    // 申请失败
                    Toast.makeText(this, "" + "权限" + permissions[i] + "申请失败", Toast.LENGTH_SHORT).show();
                }
            }
        }
    }
    public void dialPhoneNumber(String phoneNumber) {
        Intent intent = new Intent(Intent.ACTION_DIAL);
        intent.setData(Uri.parse("tel:" + phoneNumber));
        if (intent.resolveActivity(getPackageManager()) != null) {
            startActivity(intent);
        }
    }

}